package com.hcx.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hcx.reggie.exception.CustomException;
import com.hcx.reggie.mapper.CategoryMapper;
import com.hcx.reggie.pojo.Category;
import com.hcx.reggie.pojo.Dish;
import com.hcx.reggie.pojo.Setmeal;
import com.hcx.reggie.service.CategoryService;
import com.hcx.reggie.service.DishService;
import com.hcx.reggie.service.SetmealService;
import com.hcx.reggie.utils.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> implements CategoryService {
    @Autowired
    private DishService dishService;
    @Autowired
    private SetmealService setmealService;
    @Autowired
    private CategoryMapper categoryMapper;

    /**
     * 根据id删除分类
     */
    @Override
    public R<String> remove(Long ids) {
        //查询该种类是否关联了菜品
        LambdaQueryWrapper<Dish> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Dish::getCategoryId, ids);
        long count = dishService.count(wrapper);
        if (count > 0) {
            throw new CustomException("存在该种类的菜品，无法删除");
        }

        LambdaQueryWrapper<Setmeal> setmealLambdaQueryWrapper = new LambdaQueryWrapper<>();
        setmealLambdaQueryWrapper.eq(Setmeal::getCategoryId, ids);
        long count1 = setmealService.count(setmealLambdaQueryWrapper);
        if (count1 > 0) {
            throw new CustomException("存在该套餐的的菜品，无法删除");
        }
        super.removeById(ids);
        return R.success("删除成功");
    }


}
